import type { Tensor } from "@tensorflow/tfjs-node";
import { normalisedData } from "../types/normalisedData";

function normalise(
  tensor: Tensor,
  previousMin: Tensor | null = null,
  previousMax: Tensor | null = null
): normalisedData {
  const min: Tensor = previousMin || tensor.min();
  const max: Tensor = previousMax || tensor.max();
  const normalisedTensor: Tensor = tensor.sub(min).div(max.sub(min));
  return {
    tensor: normalisedTensor,
    max: max,
    min: min,
  };
}

function denormalise(tensor: Tensor, min: Tensor, max: Tensor): Tensor {
  const denomalisedTensor: Tensor = tensor.mul(max.sub(min)).add(min);
  return denomalisedTensor;
}

export { normalise, denormalise };
